package org.exam.user.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.exam.util.JsonResult;
import org.exam.user.query.UserQuery;
import org.exam.user.service.IUserService;
import org.exam.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户提示")
public class UserController {
    @ApiOperation(value = "查询单个接口")
    @PutMapping
    public JsonResult add(@RequestBody User user) {
        try {
            if (user.getId() == null) {
                iUserService.add(user);
                return JsonResult.success();
            } else {
                iUserService.update(user);
                return JsonResult.success();
            }
        } catch (Exception e) {
            return JsonResult.error();
        }
    }

    @Autowired
    public IUserService iUserService;

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Long id) {
        try {
            iUserService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/{id}")
    public User get(Long id) {
       return iUserService.get(id);
    }

    @GetMapping
    public List<User> getAll() {
        return iUserService.getAll();
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids) {
        try {
            iUserService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    @PostMapping
    public PageInfo<User> queryPage(@RequestBody UserQuery userQuery) {
        return iUserService.queryPage(userQuery);


    }
}
